package com.ruoyi.app.controller.product.controller;

import com.ruoyi.app.controller.member.mapper.IMemberMapper;
import com.ruoyi.app.controller.product.mapper.IProductMapper;
import com.ruoyi.app.controller.product.service.IProductService;
import com.ruoyi.app.framework.base.BaseAppController;
import com.ruoyi.common.annotation.AuthMember;
import com.ruoyi.common.annotation.ParseShopId;
import com.ruoyi.common.annotation.PassToken;
import com.ruoyi.common.constant.SysConfigConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.DO.member.MemberDO;
import com.ruoyi.common.entity.VO.member.MemberVO;
import com.ruoyi.common.entity.VO.product.CreditProductVO;
import com.ruoyi.common.entity.VO.product.RechargeProductVO;
import com.ruoyi.common.entity.VO.shop.ShopProductSearchVO;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/product")
@Api(value = "商品", description = "商品模块接口")
public class ProductController extends BaseAppController {

    @Resource
    private IProductService productService;

    @Resource
    private IMemberMapper memberMapper;

    /**
     * 获取充值金额列表
     */
    @GetMapping("/searchRechargeList")
    @PassToken
    public TableDataInfo searchRechargeList(@ParseShopId Long shopId,@AuthMember MemberDO member) {
        ShopProductSearchVO searchVO = new ShopProductSearchVO();
        searchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.RECHARGE.getValue());
        searchVO.setShopId(shopId);
        searchVO.setMemberVipTypeId(1L);//默认会员ID
        if (null != member && null != member.getId()) {
            MemberVO memberInfo = memberMapper.getProfileById(member.getId());
            if (null != memberInfo) {
                searchVO.setMemberVipTypeId(memberInfo.getVipTypeId());
            }
        }
        searchVO.setIsEnable(1);
        searchVO.setIsDelete(0);
        HashMap<String, Object> params = new HashMap<>();
//        if (null != shopId && 0 != shopId) {
//            Integer reviewStatus = shopAppletMapper.getReviewStatusByShopId(searchVO.getShopId());
//            if(reviewStatus == WechatConstants.ReviewStatusEnum.PASS.getValue()){
//                searchVO.setIsReview(0);
//            }else if(reviewStatus == WechatConstants.ReviewStatusEnum.AUDITING.getValue()) {
//                searchVO.setIsReview(1);
//            }
//        }
        params.put("start", ServletUtils.getParameterToInt("start", 1));
        params.put("limits", ServletUtils.getParameterToInt("limits", 200));
        BaseEntity pageVo = new BaseEntity();
        pageVo.setParams(params);
        startPage(pageVo);
        return getDataTable(productService.searchRechargeWithBenefitList(searchVO));
    }

    @ApiOperation(value = "获取积分商城商品列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "小程序appID", required = true, dataTypeClass = String.class, paramType = "header"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = ""),
            @ApiResponse(code = 500, message = "登录超时！")
    })
    @GetMapping("/searchCreditProductList")
    @PassToken
    public TableDataInfo searchCreditList(@ApiIgnore @ParseShopId Long shopId, @ApiIgnore @AuthMember MemberDO member) {
        ShopProductSearchVO searchVO = new ShopProductSearchVO();
        searchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.CREDIT.getValue());
        searchVO.setShopId(shopId);
        searchVO.setIsEnable(1);
        searchVO.setIsDelete(0);
        if (StringUtils.isNotEmpty(ServletUtils.getParameter("categoryId"))) {
            searchVO.setCategoryId(Long.parseLong(ServletUtils.getParameter("categoryId")));
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("start", ServletUtils.getParameterToInt("start", 1));
        params.put("limits", ServletUtils.getParameterToInt("limits", 200));
        params.put("orderBy", "order_num asc");
        BaseEntity pageVo = new BaseEntity();
        pageVo.setParams(params);
        startPage(pageVo);
        return getDataTable(productService.searchCreditList(searchVO));
    }

    @ApiOperation(value = "获取积分商品详情", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "小程序appID", required = true, dataTypeClass = String.class,paramType = "header"),
            @ApiImplicitParam(name = "productId", value = "商品ID", required = true, dataTypeClass = Long.class,paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = ""),
            @ApiResponse(code = 500, message = "登录超时！")
    })
    @PassToken
    @GetMapping("/getCreditDetail")
    public AjaxResult getCreditDetail(@ApiIgnore @ParseShopId Long shopId, @ApiIgnore @AuthMember MemberDO member) {
        ShopProductSearchVO searchVO = new ShopProductSearchVO();
        searchVO.setShopId(shopId);
        searchVO.setIsDelete(0);
        searchVO.setIsEnable(1);
        searchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.CREDIT.getValue());
        CreditProductVO product;
        if (StringUtils.isNotEmpty(ServletUtils.getParameter("productId"))) {
            searchVO.setProductId(Long.parseLong(ServletUtils.getParameter("productId")));
            product = productService.getCreditDetail(searchVO);
        } else {
            return error("商品详情获取失败！");
        }
        return AjaxResult.success(product);
    }

}
